Turn the JavaScript code into a separate CoffeeScript file.

Akinori MUSHA 9 anos atrás
pai
commit
0c9c862c53

+ 20 - 0
app/assets/javascripts/diagram.js.coffee

@@ -0,0 +1,20 @@
1
+$ ->
2
+  svg = document.querySelector('.agent-diagram svg.diagram')
3
+  overlay = document.querySelector('.agent-diagram .overlay')
4
+  getTopLeft = (node) ->
5
+    bbox = node.getBBox()
6
+    point = svg.createSVGPoint()
7
+    point.x = bbox.x + bbox.width
8
+    point.y = bbox.y
9
+    point.matrixTransform(node.getCTM())
10
+  $(svg).find('g.node[data-badge-id]').each ->
11
+    tl = getTopLeft(this)
12
+    $('#' + this.getAttribute('data-badge-id'), overlay).each ->
13
+      badge = $(this)
14
+      badge.css
15
+        left: tl.x - badge.outerWidth()  * (2/3)
16
+        top:  tl.y - badge.outerHeight() * (1/3)
17
+        'background-color': badge.find('.label').css('background-color')
18
+      .show()
19
+      return
20
+    return

+ 1 - 27
app/helpers/dot_helper.rb

@@ -214,33 +214,7 @@ module DotHelper
214 214
           }
215 215
         }
216 216
       }
217
-
218
-      root << Nokogiri::XML::Node.new('script', doc) { |script|
219
-        script.content = <<-SCRIPT
220
-$(function () {
221
-  var svg = document.querySelector('.agent-diagram svg.diagram');
222
-  var overlay = document.querySelector('.agent-diagram .overlay');
223
-  var getTopLeft = function (node) {
224
-    var bbox = node.getBBox();
225
-    var point = svg.createSVGPoint();
226
-    point.x = bbox.x + bbox.width;
227
-    point.y = bbox.y;
228
-    return point.matrixTransform(node.getCTM());
229
-  };
230
-  $(svg).find('g.node[data-badge-id]').each(function () {
231
-    var tl = getTopLeft(this)
232
-    $('#' + this.getAttribute('data-badge-id'), overlay).each(function () {
233
-      var badge = $(this);
234
-      badge.css({
235
-        left: tl.x - badge.outerWidth()  * (2/3),
236
-        top:  tl.y - badge.outerHeight() * (1/3),
237
-        'background-color': badge.find('.label').css('background-color')
238
-      }).show();
239
-    });
240
-  });
241
-})
242
-        SCRIPT
243
-      }
217
+      # See also: app/assets/diagram.js.coffee
244 218
     }.at('div.agent-diagram').to_s
245 219
   end
246 220
 end

+ 4 - 0
app/views/diagrams/show.html.erb

@@ -1,3 +1,7 @@
1
+<% content_for :head do %>
2
+  <%= javascript_include_tag "diagram" %>
3
+<% end %>
4
+
1 5
 <div class='container'>
2 6
   <div class='row'>
3 7
     <div class='col-md-12'>

+ 1 - 1
config/environments/production.rb

@@ -61,7 +61,7 @@ Huginn::Application.configure do
61 61
   end
62 62
 
63 63
   # Precompile additional assets (application.js.coffee.erb, application.css, and all non-JS/CSS are already added)
64
-  config.assets.precompile += %w( graphing.js user_credentials.js )
64
+  config.assets.precompile += %w( diagram.js graphing.js user_credentials.js )
65 65
 
66 66
   # Ignore bad email addresses and do not raise email delivery errors.
67 67
   # Set this to true and configure the email server for immediate delivery to raise delivery errors.